package cn.com.elite;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.jdbc.core.JdbcTemplate;

import com.elitecrm.saas.service.AbstractService;
import com.elitecrm.saas.service.CommonService;
import com.elitecrm.saas.service.EliteSAASException;

public class KSPXSerciveImpl extends AbstractService{
	private static final Log log = LogFactory.getLog(KSPXSerciveImpl.class);
	
	public String callService(String param) throws Exception{
		try{
			return callServiceImpl(param);
		}catch(Exception e){
			log.error("callService_wrong"+util.getERRO(e));
			throw e;
		}
	}
	public String callServiceImpl(String param) throws Exception{
		log.debug("param:"+param);
		String ReturnXml="";
		Document doc=DocumentHelper.parseText(param);
		Element uuidEle=(Element)doc.selectSingleNode("/Root/UUID");
		if(uuidEle!=null){
			String uuid=uuidEle.getText();
			log.debug("uuid:"+uuid);
			
			String lorixml=checkUUID(uuid);
			log.debug("lorixml:"+lorixml);
			Document checkResultDoc=DocumentHelper.parseText(lorixml);
			String checkResult=checkResultDoc.selectSingleNode("/R/Result").getText();
			String ep_id=checkResultDoc.selectSingleNode("/R/ep_id").getText();
			log.debug("ep_id:"+ep_id);
			String staffid=checkResultDoc.selectSingleNode("/R/staff_id").getText();
			log.debug("staffid:"+staffid);
			
			if("1".equals(checkResult)){
				Element actionEle=(Element)doc.selectSingleNode("/Root/action");
				if(actionEle!=null){
					String action=actionEle.getText();
					log.debug("action:"+action);
					String HandleGroup_ID="SYSTEM";
					log.debug("HandleGroup_ID:"+HandleGroup_ID);
					if(action!=null){
						log.debug("action:"+action);
						if(action.equals("getPXListService")){
							GetPXListService getPXListService=new GetPXListService();
							ReturnXml=getPXListService.getPXListService(getDataSource(),ep_id,doc,staffid);
						}else if(action.equals("getKSLXService")){
							GetKSLXService getKSLXService=new GetKSLXService();
							ReturnXml=getKSLXService.getKSLXService(getDataSource(),ep_id,doc,staffid);
						}else if(action.equals("loadMeInfoService")){
							LoadMeInfoService loadMeInfoService=new LoadMeInfoService();
							ReturnXml=loadMeInfoService.loadMeInfoService(getDataSource(),ep_id,doc,HandleGroup_ID,staffid);
						}else if(action.equals("saveInfoService")){
							SaveInfoService saveInfoService=new SaveInfoService();
							ReturnXml=saveInfoService.saveInfoService(getDataSource(),ep_id,doc,staffid);
						}else if(action.equals("queryInfoService")){
							QueryInfoService queryInfoService=new QueryInfoService();
							ReturnXml=queryInfoService.queryInfoService(getDataSource(),ep_id,staffid,doc);
						}else if(action.equals("loadkscomments")){
							Loadkscomments loadkscomments=new Loadkscomments();
							ReturnXml=loadkscomments.loadkscomments(getDataSource(), ep_id, doc, HandleGroup_ID, staffid);
						}else if(action.equals("queryExamLevel")){
							QueryExamLevel queryExamLevel=new QueryExamLevel();
							ReturnXml=queryExamLevel.queryExamLevel(getDataSource(), ep_id);
						}else if(action.equals("loadPXInfoService")){
							LoadPXInfoService loadPXInfoService=new LoadPXInfoService();
							ReturnXml=loadPXInfoService.loadPXInfoService(getDataSource(), ep_id, doc, HandleGroup_ID, staffid);
						}else if(action.equals("endPXService")){
							EndPXService endPXService=new EndPXService();
							ReturnXml=endPXService.endPXService(getDataSource(), ep_id, doc, HandleGroup_ID, staffid);
						}else{
							throw new EliteSAASException(EliteSAASException.INVAILD_ACTION);
						}
					}else{
						throw new EliteSAASException(EliteSAASException.INVAILD_ACTION);
					}
				}else{
					throw new EliteSAASException(EliteSAASException.INVAILD_ACTION);
				}
			}else{
				throw new EliteSAASException(EliteSAASException.INVAILD_UUID);
			}
		}else{
			throw new EliteSAASException(EliteSAASException.NO_UUID_FOUND);
		}
		return ReturnXml;
	}

}
